using HEFramework;
using UnityEditor;
using UnityEngine;

/// <summary>
///
/// 组件行为编辑面板
/// 
/// ----------------------------------------
/// 作者 hep
/// 创建时间：2023-04-30 17:45:30
/// ----------------------------------------
/// </summary>
[CustomEditor(typeof(TransformBehaviour))]
public class TransformBehaviourInspector : Editor
{
    public override void OnInspectorGUI()
    {
        GUILayout.BeginHorizontal();
        TransformBehaviour transformBehaviour = target as TransformBehaviour;
        if (GUILayout.Button(new GUIContent("组件绑定"), EditorStyles.miniButton, GUILayout.Width(130)))
        {
            transformBehaviour.Transforms.Clear();
            GameObject[] objs = Selection.GetFiltered<GameObject>(SelectionMode.Deep);
            for (int i = 0; i < objs.Length; ++i)
            {
                GameObject obj = objs[i];
                if (obj.name.StartsWith("Transform_") || obj.name.StartsWith("transform_"))
                {
                    if (CheckName(transformBehaviour, obj.name))
                    {
                        transformBehaviour.Transforms.Add(obj.transform);
                    }
                }
            }

            PrefabUtility.SavePrefabAsset(Selection.activeGameObject);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Debug.Log("绑定组件完成！");
        }

        GUILayout.EndHorizontal();
        base.OnInspectorGUI();
    }

    private bool CheckName(TransformBehaviour _transformBehaviour, string _name)
    {
        for (int i = 0; i < _transformBehaviour.Transforms.Count; i++)
        {
            if (_transformBehaviour.Transforms[i].name == _name)
            {
                Debug.LogError(_transformBehaviour.name + "-->" + _name + " exist.");
                return false;
            }
        }

        return true;
    }
}